<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Beard's Burgers and Subs</title>
    </head>
    <body>
        <h1>Your order is:</h1>
<?php


// 1. Collect the input from the form.
//              Prep the input
$_POST['fname'] = trim($_POST['fname']);
$_POST['lname'] = trim($_POST['lname']);
$_POST['address']  = trim($_POST['address']);
$_POST['phone']  = trim($_POST['phone']);
$_POST['note']  = trim($_POST['note']);
$number_of_toppings = count($_POST['top']);
$base_price = 5;
// 2. Validating the input
//
$required_fields = array('fname', 'lname', 'address','phone');

// 2.1 Ensure that the user typed in the fields we want
foreach($required_fields as $field)
{
       // echo "<p>Checking to ensure $field was submitted...";
        if ( ! ensure_field_was_submitted($field))
                exit(show_error("You need to enter your $field"));
}

// 2.2 Ensure that some of the fields meet the length requirement
if ( ! ensure_field_is_correct_length('fname'))
        exit(show_error("The First Name should be less than 50 characters"));
if ( ! ensure_field_is_correct_length('lname'))
        exit(show_error("The Last Name should be less than 50 characters"));
if ( ! ensure_field_is_correct_length('address', 100))
        exit(show_error("The Address should be less than 100 characters"));
if ( ! ensure_field_is_correct_length('phone', 15))
        exit(show_error("The phone should be less than 15 numbers"));

function show_error($message)
{
    echo "<p> $message </p>";
    
}


function ensure_field_was_submitted($field_name)
{
        if (isset($_POST[$field_name]) && $_POST[$field_name] != '')
                return TRUE;
        else
                return FALSE;
}

function ensure_field_is_correct_length($field_name, $max_length = 50)
{
        if (strlen($_POST[$field_name]) < $max_length)
                return TRUE;
        else
                return FALSE;
}

function price($number_of_toppings, $base_price )
{
    $total_price = $base_price + ($number_of_toppings * .5);
    echo "<p> Total Price: $total_price  </p>";
    
}



echo "<p>Name: {$_POST['fname']} {$_POST['lname']} </p>";
echo "<p>Address: {$_POST['address']}</p>";
echo "<p>City: {$_POST['city']}</p>";
echo "<p>Phone Number: {$_POST['phone']}</p>";
echo "<p>Sub: {$_POST['sub']}</p>";
echo "<p>Topping: {$_POST['top']}</p>";
echo "<p>Drink: {$_POST['drink']}</p>";
price($number_of_toppings, $base_price );
echo "<p>Additional Instructions: {$_POST['note']}</p>";

/* EOF */
?>

 </body>
</html>